//
//  SunGetMoneyVC.m
//  Xspcf
//
//  Created by sunshine on 16/10/28.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunGetMoneyVC.h"
#import "SunCheckGetMoneyVC.h"
#import "SunRecordVC.h"
#import "JxWebVC.h"
#import "SunWebVC.h"
#import "SunDelegateWebVC.h"
@interface SunGetMoneyVC ()<UITextFieldDelegate>
{
    UILabel *_leaveMoney;
    UIImageView *_bankLogo;
    UILabel *_bankName;
    UITextField *_money;
    UILabel *_fee;
    UILabel *_timeLabel;
    UIButton *_next;
}
@property (nonatomic, strong) UILabel *realMoney;
@property (nonatomic, strong) Sun *sunAlert;

@end

@implementation SunGetMoneyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BACKGROUND;
    self.title = @"我要提现";
    [self creatUI];
    [self updateViews];
    self.sunAlert = [[Sun alloc]init];

    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestToUserInfo];
}

- (void)creatUI{
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, X(25), 25)];
    [btn setImage:[UIImage imageNamed:@"tool_record"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(gotoRecord) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barbutton = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = barbutton;
    
    UIView *cardBackImage = [[UIView alloc]init];
    cardBackImage.backgroundColor = WHITE;
    [cardBackImage setFrame:CGRectMake(0, -5, SCREENWIDTH, 55)];
    [self.view addSubview:cardBackImage];
    // 银行图标
    _bankLogo = [[UIImageView alloc]init];
    [cardBackImage addSubview:_bankLogo];
    [_bankLogo setFrame:CGRectMake(X(94), 20, X(20), X(20))];
    _bankLogo.contentMode = UIViewContentModeScaleAspectFit;
    // 银行 尾号
    _bankName = [[UILabel alloc]init];
    [cardBackImage addSubview:_bankName];
    [_bankName setFrame:CGRectMake(X(120), 19, 200, 20)];
    _bankName.textColor = [UIColor colorWithHexString:@"565D66"];
    _bankName.font = [UIFont systemFontOfSize:14];

    UIImageView *moneyImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"lose_bg"]];
    [moneyImageView setFrame:CGRectMake(0, 77, SCREENWIDTH, 161)];
    [self.view addSubview:moneyImageView];
    moneyImageView.userInteractionEnabled = YES;
    // 人民币标志
    UILabel *rmbLabel = [[UILabel alloc]initWithFrame:CGRectMake(X(20), 22, X(20), 28)];
    rmbLabel.text = @"¥";
    rmbLabel.textColor = [UIColor colorWithHexString:@"050505"];
    rmbLabel.font = [UIFont systemFontOfSize:20];
    [moneyImageView addSubview:rmbLabel];
    // 提现金额输入框
    _money = [[UITextField alloc]init];
    [moneyImageView addSubview:_money];
    [_money setFrame:CGRectMake(X(56), 19, X(200), 33)];
    _money.font = [UIFont systemFontOfSize:24];
    _money.clearButtonMode = UITextFieldViewModeWhileEditing;
    _money.keyboardType = UIKeyboardTypeDecimalPad;
    _money.placeholder = @"请输入提现金额";
    _money.delegate = self;
    [_money addTarget:self action:@selector(updateRealMoney) forControlEvents:UIControlEventEditingDidEnd];
    
    // 全部提现按钮
    UIButton *allBtn = [[UIButton alloc]init];
    [moneyImageView addSubview:allBtn];
    [allBtn lt_setSize:CGSizeMake(100, 30) save:YES];
    [allBtn lt_setRight:X(14) to:nil save:YES];
    [allBtn lt_YcenterTo:_money save:YES];
    [allBtn setTitleColor:[UIColor colorWithHexString:@"F96C5F"] forState:UIControlStateNormal];
    [allBtn setTitle:@"全部提现" forState:UIControlStateNormal];
    allBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18];
    [allBtn addTarget:self action:@selector(allMoneyAction) forControlEvents:UIControlEventTouchUpInside];
    
    // 账户余额
    UILabel *l1 = [[UILabel alloc]init];
    [moneyImageView addSubview:l1];
    [l1 setFrame:CGRectMake(X(24), 83, 76, 20)];
    l1.textColor = [UIColor colorWithHexString:@"A2A2A2"];
    l1.font = [UIFont systemFontOfSize:14];
    l1.text = @"账户余额";
    // 余额
    _leaveMoney = [[UILabel alloc]init];
    [moneyImageView addSubview:_leaveMoney];
    [_leaveMoney setFrame:CGRectMake(X(110),83, 200, 20)];
    _leaveMoney.textColor = [UIColor colorWithHexString:@"A2A2A2"];
    _leaveMoney.font = [UIFont systemFontOfSize:14];
    // 提现手续费
    UILabel *cutWord = [[UILabel alloc]init];
    [moneyImageView addSubview:cutWord];
    [cutWord setFrame:CGRectMake(X(24), 110, 90, 20)];
    cutWord.text = @"提现手续费";
    cutWord.textColor = [UIColor colorWithHexString:@"A2A2A2"];
    cutWord.font = [UIFont systemFontOfSize:14];
    _fee = [[UILabel alloc]init];
    [moneyImageView addSubview:_fee];
    [_fee setFrame:CGRectMake(X(110), 110, 200, 20)];
    _fee.textColor = [UIColor colorWithHexString:@"A2A2A2"];
    _fee.font = [UIFont systemFontOfSize:14];
    // ？按钮
    UIButton *ques = [[UIButton alloc]init];
    [moneyImageView addSubview:ques];
    [ques setFrame:CGRectMake(SCREENWIDTH- X(38), 111, 16, 16)];
    ques.layer.cornerRadius = 8;
    ques.clipsToBounds = YES;
    [ques setBackgroundColor:[UIColor colorWithHexString:@"D8D8D8"] forState:UIControlStateNormal];
    [ques setTitle:@"?" forState:UIControlStateNormal];
    [ques setTitleColor:WHITE forState:UIControlStateNormal];
    [ques addTarget:self action:@selector(question) forControlEvents:UIControlEventTouchUpInside];
    // 实际到账金额
    UILabel *label1 = [[UILabel alloc]init];
    [moneyImageView addSubview:label1];
    [label1 setFrame:CGRectMake(X(24), 140, 76, 20)];
    label1.text = @"实际到账";
    label1.textColor = [UIColor colorWithHexString:@"A2A2A2"];
    label1.font = [UIFont systemFontOfSize:14];
    self.realMoney = [[UILabel alloc]init];
    [moneyImageView addSubview:self.realMoney];
    [self.realMoney setFrame:CGRectMake(X(110), 140, 200, 20)];
    self.realMoney.font = [UIFont systemFontOfSize:14];
    self.realMoney.textColor = [UIColor colorWithHexString:@"FFA887"];
    // 剩余次数
    _timeLabel = [[UILabel alloc]init];
    [self.view addSubview:_timeLabel];
    _timeLabel.font = [UIFont systemFontOfSize:12];
    _timeLabel.textColor = [UIColor colorWithHexString:@"B2B1B1"];
    [_timeLabel setFrame:CGRectMake(X(19), 249, 200, 17)];
    
    // 提现说明
    UIButton *protocol = [[UIButton alloc]init];
    [self.view addSubview:protocol];
    [protocol setFrame:CGRectMake(SCREENWIDTH - X(73), 249, 58, 17)];
    [protocol setTitle:@"提现规则" forState:UIControlStateNormal];
    [protocol setTitleColor:[UIColor colorWithHexString:@"000000"] forState:UIControlStateNormal];
    protocol.titleLabel.font = [UIFont systemFontOfSize:12];
    [protocol doclick:^{
        SunWebVC *vc = [[SunWebVC alloc]init];
        vc.urlStr = [NSString stringWithFormat:@"%@/More/getArticleByNid?nid=cash_declaration&type=1&source=ios",KWAP];
        vc.titleName = @"提现规则";
        [self.navigationController pushViewController:vc animated:YES];
    }];

    _next = [[UIButton alloc]init];
    [self.view addSubview:_next];
    [_next lt_Top:SunEdgesMake(132, X(15), X(15), 44) to:moneyImageView save:YES];
    _next.clipsToBounds = YES;
    [_next setBackgroundImage:[UIImage imageNamed:@"button_bg"] forState:UIControlStateNormal];
//    _next.layer.cornerRadius = 4;
    [_next setTitle:@"申请提现" forState:UIControlStateNormal];
    [_next setTitleColor:WHITE forState:UIControlStateNormal];
    _next.titleLabel.font = [UIFont systemFontOfSize:18];
    [_next addTarget:self action:@selector(gotoGetMoney) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *safeImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"safe_icon"]];
    [safeImage setFrame:CGRectMake(X(108),550, 12, 14)];
    [self.view addSubview:safeImage];
    UILabel *safeLable = [[UILabel alloc]initWithFrame:CGRectMake(X(125), 548, 200, 17)];
    safeLable.text = [Sun getUser:SAFETEXT];
    safeLable.textColor = [UIColor colorWithHexString:@"000000"];
    safeLable.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:safeLable];
}
- (void)updateRealMoney{
    NSDictionary *dic = [Sun getUser:USER];
    CGFloat wantMoney = _money.text.doubleValue;
    CGFloat feeMoney = [[dic objectForKey:@"cashFee"]doubleValue];
    self.realMoney.text = [NSString stringWithFormat:@"%.2f元",wantMoney-feeMoney];
}
- (void)updateViews{
    NSDictionary *dic = [Sun getUser:USER];
    _timeLabel.text = [NSString stringWithFormat:@"剩余免费次数：%d次",[[dic objectForKey:@"cashFreeNum"]intValue]];
    _leaveMoney.text= [NSString stringWithFormat:@"%.2lf元",[[dic objectForKey:@"balanceMoney"] doubleValue]];
    [_bankLogo sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KIMG,[dic objectForKey:@"bankImg"]]]];
    _bankName.text = [NSString stringWithFormat:@"%@(尾号%@)",[dic objectForKey:@"bank"],[[dic objectForKey:@"bankCard"] substringFromIndex:[[dic objectForKey:@"bankCard"]length] - 4]];
    _fee.text = [NSString stringWithFormat:@"%.2f元",[[dic objectForKey:@"cashFee"]doubleValue]];
    self.realMoney.text = @"0.00元";
}
#pragma mark- 全部提现
- (void)allMoneyAction {
    NSDictionary *dic = [Sun getUser:USER];
    _money.text = [NSString stringWithFormat:@"%.2f",[[dic objectForKey:@"balanceMoney"] doubleValue]];
    [self updateRealMoney];
}
- (void)gotoRecord{
    SunRecordVC *vc = [[SunRecordVC alloc]init];
    vc.type = 1;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)requestToUserInfo{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_USERINFO];
    [HttpRequest postURL:url withParam:nil success:^(id requestDic, NSString *msg) {
        [Sun setUser:requestDic forKey:USER];
        [self updateViews];
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]showMsg:errorInfo];
    }];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *can = [_leaveMoney.text stringByReplacingOccurrencesOfString:@"," withString:@""];
    NSString *result = textField.text;
    if (string.length > 0 && textField.text.length < 6) {
        result = [textField.text stringByAppendingString:string];
    }else if (string.length <= 0 && textField.text.length > 0){
        result = [textField.text substringToIndex:textField.text.length - 1];
    }
    if (result.doubleValue >= can.doubleValue && string.length > 0) {
        textField.text = can;
        return NO;
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    textField.text = [NSString stringWithFormat:@"%.2f",textField.text.doubleValue];
    if (textField.text.doubleValue == 0) {
        textField.text = @"";
    }
}

- (void)gotoGetMoney{
    NSDictionary *dic = [Sun getUser:USER];
    if (_money.text.doubleValue == 0) {
        [[Sun shareSun]showMsg:@"请输入提现金额"];
        return;
    }else if([[dic objectForKey:@"bankCodeStatus"]intValue] != 1 && [[dic objectForKey:@"company"]intValue] == 1){
        [SunSupport shouldAlert:self code:UserCompany];
    }else {
        NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_CASH];
        [[Sun shareSun]showActivity:self];
        
        SunDelegateWebVC *vc = [[SunDelegateWebVC alloc]init];
        NSDictionary *param = @{@"device":@"iOS",@"amount":_money.text};
        NSString *str = [Sun stringFromDictionary:(NSDictionary *)param];
        vc.params = str;
        vc.urlStr = url;
        vc.type =  GetCash;
        vc.title = @"提现";
        [self.navigationController pushViewController:vc animated:YES];
        
    }
}

- (void)question{
//    [[Sun shareSun]alertAlign:NSTextAlignmentLeft title:@"提现手续费" content:@"1、每月前三笔提现免费\n2、每月第四笔提现开始，按2元/笔进行收取\n3、提现手续费是第三方支付平台向用户收取的费用" left:@"知道了" right:nil cancel:^{
//        [[Sun shareSun]hideAll];
//    } sure:nil To:self];
    UIView *backView = [[UIView alloc]init];
    backView.backgroundColor = [UIColor clearColor];
    UIView *alertView = [[UIView alloc]init];
    alertView.backgroundColor = [UIColor whiteColor];
    alertView.layer.cornerRadius = 5;
    [alertView setFrame:CGRectMake(0, 35, X(300), 210)];
    [backView addSubview:alertView];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    [alertView addSubview:titleLabel];
    [titleLabel lt_XcenterTo:nil save:YES];
    [titleLabel lt_setTop:11 to:nil save:YES];
    titleLabel.text = @"提示";
    titleLabel.textColor = [UIColor colorWithHexString:@"4B4B4B"];
    titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18];
    
    UILabel *label1= [[UILabel alloc]init];
    [alertView addSubview:label1];
    label1.text = @"● 每月前三笔提现免费";
    label1.textColor = [UIColor colorWithHexString:@"737170"];
    [label1 lt_setTop:20 to:titleLabel save:YES];
    [label1 lt_setLeft:X(20) to:nil save:YES];
    label1.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    
    UILabel *label2 = [[UILabel alloc]init];
    [alertView addSubview:label2];
    label2.text = @"● 每月第四笔提现开始，按3元/笔进行收取";
    label2.textColor = [UIColor colorWithHexString:@"737170"];
    [label2 lt_setTop:14 to:label1 save:YES];
    [label2 lt_setLeft:X(20) to:nil save:YES];
    label2.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];

    UILabel *label3 = [[UILabel alloc]init];
    [alertView addSubview:label3];
    label3.text = @"● 提现手续费是第三方支付平台向用户收取的费用";
    label3.textColor = [UIColor colorWithHexString:@"737170"];
    [label3 lt_setTop:14 to:label2 save:YES];
    [label3 lt_setLeft:X(20) to:nil save:YES];
    label3.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    
    UIButton *knowButton = [[UIButton alloc]init];
    [knowButton setFrame:CGRectMake(X(8), 156, X(300) - X(16), 44)];
    [alertView addSubview:knowButton];
    [knowButton setBackgroundImage:[UIImage imageNamed:@"button_alert"] forState:UIControlStateNormal];
    [knowButton setTitle:@"我知道了" forState:UIControlStateNormal];
    [knowButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    knowButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18];
    [knowButton addTarget:self action:@selector(hideAlert) forControlEvents:UIControlEventTouchUpInside];
    
    [self.sunAlert show:backView to:self shouldReturn:YES];
    
    [backView lt_setSize:CGSizeMake(X(300), 310) save:YES];
    [backView lt_XYcenterTo:nil save:YES];
}
- (void)hideAlert {
    [self.sunAlert hideAll];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
